using System;
using System.IO;
using System.Globalization;
using System.Collections;
using System.Threading;
using SharePoint.ImportExport.Core;

namespace SharePointExporter.FileSystem
{
	/// <summary>
	/// This is the class for managing the command line arguments
	/// </summary>
	public class FileParams
	{
		private string startDirectory="";
        private string compressedDirectory = "";
        private ArrayList inclusions;
        private ArrayList exclusions;
        private DateTime datetimeThreshhold;
        private SiteType containerType = SiteType.Site;
        private string siteName = "Site";
        
        private string documentLibraryName = "";
        private string managedpath = "Sites";
		
        private bool dateSpecified = false;
        private bool enableVersions = true;

        public string ManagedPath
        {
            get
            {
                return this.managedpath;
            }
            set
            {
                this.managedpath = value;
            }
        }

		public string StartDirectory
		{
			get
			{
				return startDirectory;
			}
            set
            {
                startDirectory = value;
            }

		}

		
		public ArrayList Inclusions
		{
			get
			{
				return inclusions;
			}
            set
            {
                inclusions = value;
            }

		}
		
		public ArrayList Exclusions
		{
			get
			{
				return exclusions;
			}
            set
            {
                exclusions = value;
            }

		}

		public DateTime DateTimeThreshhold
		{
			get
			{
				return datetimeThreshhold;
			}
            set
            {
                datetimeThreshhold = value;
            }
        }

		public bool DateSpecified
		{
			get
			{
				return dateSpecified;
			}
            set
            {
                dateSpecified = value;
            }
		}
        
		public bool EnableVersions
		{
			get
			{
				return enableVersions;
			}
            set
            {
                enableVersions = value;
            }

		}

        public SiteType ContainerType
		{
			get
			{
				return containerType;
			}
            set
            {
                containerType = value;
            }

		}

        public string SiteName
		{
			get
			{
				return siteName;
			}
            set
            {
                siteName = value;
            }

		}

        public string DocumentLibraryName
		{
			get
			{
				return documentLibraryName;
			}
			set
			{
				documentLibraryName=value;
			}
		}
                
        public string CompressedDirectory
        {
            get
            {
                return compressedDirectory;
            }
            set
            {
                compressedDirectory = value;
            }
        }

        private string siteDefinition="STS#0";

        public string SiteDefinition
        {
            get
            {
                return this.siteDefinition;
            }
            set
            {
                this.siteDefinition = value;
            }
        }

        private bool compressed;

        public bool Compressed
        {
            get
            {
                return this.compressed;
            }
            set
            {
                this.compressed = value;
            }
        }

		private string owner=Thread.CurrentPrincipal.Identity.Name;
		public string Owner
		{
			get
			{
				return owner;
			}
			set
			{
				owner=value;
			}
		}

		private string contact="";
		public string Contact
		{
			get
			{
				return contact;
			}
            set
            {
                contact = value;
            }

		}
        private string outputDirectory = "";
        public string OutputDirectory
        {
            get
            {
                return outputDirectory;
            }
            set
            {
                outputDirectory = value;
            }

        }
        private string logDirectory = "";
        public string LogDirectory
        {
            get
            {
                return logDirectory;
            }
            set
            {
                logDirectory = value;
            }

        }
        private string mode = "single";
		public string Mode
		{
			get
			{
				return mode;
			}
            set
            {
                mode = value;
            }

		}
		private ErrorLevel reportLevel=ErrorLevel.Information;
		public ErrorLevel ReportLevel
		{
			get
			{
				return reportLevel;
			}
            set
            {
                reportLevel = value;
            }

		}
        public bool Serialize(string fileName)
        {
            using (StreamWriter sw = new StreamWriter(fileName))
            {
                sw.WriteLine(this.Contact);
                sw.WriteLine(this.ContainerType.ToString());
                sw.WriteLine(this.DateSpecified.ToString());
                sw.WriteLine(this.DateTimeThreshhold.ToString());
                sw.WriteLine(this.DocumentLibraryName);
                sw.WriteLine(this.EnableVersions.ToString());
                if (this.Exclusions == null)
                    sw.WriteLine("");
                else
                    sw.WriteLine(this.Exclusions.ToString());
                if (this.Inclusions== null)
                    sw.WriteLine("");
                else
                    sw.WriteLine(this.Inclusions.ToString());
                sw.WriteLine(this.Mode);
                sw.WriteLine(this.OutputDirectory);
                sw.WriteLine(this.Owner);
                sw.WriteLine(this.ReportLevel.ToString());
                sw.WriteLine(this.ManagedPath);
                sw.WriteLine(this.SiteName);
                sw.WriteLine(this.StartDirectory);
                sw.WriteLine(this.logDirectory);

                sw.Flush();
                sw.Close();

            }
            return true;

        }
        public bool Deserialize(string fileName)
        {
            bool retvalue = false;
            try
            {
                using (StreamReader sr = new StreamReader(fileName))
                {
                    this.Contact = sr.ReadLine();
                    string s = sr.ReadLine();
                    this.ContainerType = (SiteType)Enum.Parse(typeof(SiteType), s);
                    s = sr.ReadLine();
                    this.DateSpecified = Boolean.Parse(s);
                    s = sr.ReadLine();
                    this.DateTimeThreshhold = DateTime.Parse(s);
                    this.DocumentLibraryName = sr.ReadLine(); ;
                    s = sr.ReadLine();
                    this.EnableVersions = Boolean.Parse(s);
                    s = sr.ReadLine();
                    s = sr.ReadLine();
                    this.Mode = sr.ReadLine();
                    this.OutputDirectory = sr.ReadLine();
                    this.Owner = sr.ReadLine();
                    s = sr.ReadLine();
                    this.ManagedPath = sr.ReadLine();
                    this.SiteName = sr.ReadLine();
                    this.StartDirectory = sr.ReadLine();
                    this.logDirectory = sr.ReadLine();
                    retvalue = true;

                    sr.Close();

                }
            }
            catch
            {
            }
            return retvalue;

        }
		
		public bool ProcessArgs(string[] args)
		{
			datetimeThreshhold=DateTime.Today;
			bool bRetValue=false;
			for(int i=0;i<args.GetLength(0)-1;i+=2)
			{
				string lowerarg=args[i];
				string data=args[i+1];
				string[] fileextensions;
				switch(lowerarg)
				{

					case "-inclusions":
						fileextensions=data.Split(';');
						foreach(string extension in fileextensions)
							inclusions.Add(extension);
						break;
                    case "-exclusions":
                        fileextensions = data.Split(';');
                        foreach (string extension in fileextensions)
                            exclusions.Add(extension);
                        break;
                    case "-sitedefinition":
                        {
                            this.siteDefinition = data;
                            break;
                        }
                    case "-reportLevel":
					{
						string stringReportLevel=args[i+1];
						if(stringReportLevel.Length>0)
							reportLevel=(ErrorLevel)Enum.Parse(typeof(ErrorLevel), stringReportLevel);
						break;
					}
                    case "-managedpath":
                    {
                        this.ManagedPath= args[i + 1];
                        break;
                    }
                    case "-folder":
						startDirectory=args[i+1];
						break;
					case "-mode":
						mode=args[i+1];
						break;
					case "-out":
						outputDirectory=args[i+1];
						break;
					case "-library":
						documentLibraryName=args[i+1];
						break;
					
					case "-name":
						siteName=args[i+1];
						break;
					case "-owner":
						owner=args[i+1];
						break;
					case "-contact":
						contact=args[i+1];
						break;
					case "-datethreshold":
						datetimeThreshhold=Convert.ToDateTime(args[i+1],CultureInfo.InvariantCulture);
						dateSpecified=true;
						break;
				}
			}
			if(startDirectory.Length==0 || siteName.Length==0 || containerType==SiteType.Undefined)
			{
				bRetValue=false;
			}
			else
				bRetValue=true;



			return bRetValue;

		}

        public FileParams()
        {
            inclusions = new ArrayList();
            exclusions = new ArrayList();
        }
	}
}
